package com.baosight.sczx.zm.cl.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cl.RoundCheckWorkNote;
import com.baosight.sczx.zm.cl.domain.ZMCLSecurityPath;

/**
 * 巡检路线管理 
 * <p>Title: ServiceZMCL91.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-3-30   高强   Create
 *</pre>
 */
public class ServiceZMCL91 extends ServiceEPBase {
	
	/**
	 * 巡检路线管理 进入初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-30              高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(getFrequencyUnits());//获得频率类型
		outInfo.addBlock(getStatus(true));//获得状态类型
		String pathCode = this.getSurveyPathCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdPathCode");
		outInfo.set("fdPathCode", pathCode);//新增时按照规则生成最新的巡检路线编号
		String defaultFrequencyType = initCombox(getFrequencyUnits(), 0);
		outInfo.set("defaultFrequencyType", defaultFrequencyType);
		return outInfo;
	}
	
	/**
	 * 巡检路线 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-3-30              高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo)
	{
		EiInfo queryInfo = super.query(inInfo, "ZMCLSecurityPath.query", new ZMCLSecurityPath());
		EiInfo outInfo = getNoteCode(queryInfo);//获得巡检工单号
		return outInfo;
	}
	
	/**
	 * 查询最新的巡检路线编号 
	 * <p>Function: getSurveyPathCode</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-3-30              高强       
	 * </p>
	 *
	 */
	

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public EiInfo getSurveyPathCode(EiInfo inInfo)
	{
		List<Map>  _list = dao.query("ZMCLSecurityPath.queryLatestSurveyPathCode", null);
		EiInfo outInfo = new EiInfo();
		outInfo.addBlock(EiConstant.resultBlock).addBlockMeta(new ZMCLSecurityPath().eiMetadata);
		if(_list.size() == 0)
		{
			String defaultStr = "AQXJLX" + "_" + StaticTools.getDateDefault()+ "_" + "001";//如果在当天没有记录生成，则默认为是当天的第一条记录
			outInfo.getBlock("result").setCell(0,"fdPathCode", defaultStr);
		}else
		{
			String str = _list.get(0).get("fdPathCode").toString();
			outInfo.getBlock("result").setCell(0,"fdPathCode", StaticTools.convertStr(str, 16, 19));//编号的前15位AQXJLX_YYYYMMDD_,全长为19位
		}
		return outInfo;
	}
	
	/**
	 * 巡检路线 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-30              高强         
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdPathCode", inInfo.getBlock("result").getCellStr(0, "fdPathCode"));
		boolean validateValue = validate(inInfo,params);//有效性验证
		if(validateValue) {
			return inInfo;
		}
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CL_SECURITYPATH"));//主键
		inInfo.getBlock("result").setCell(0, "fdStatus", "在用");//新增状态默认为在用
		EiInfo outInfo = super.insert(inInfo, "ZMCLSecurityPath.insert", "result");
		String pathCode = this.getSurveyPathCode(inInfo).getBlock(EiConstant.resultBlock).getCellStr(0, "fdPathCode");
		outInfo.set("fdPathCode", pathCode);//新增时按照规则生成巡检路线编号
		return outInfo;
	}
	
	/**
	 * 巡检路线 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-30              高强  
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCLSecurityPath.update");
		return outInfo;
	}
	
    /**
	 * 构造状态下拉框的数据
	 * <p>Function: getStatus</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-30下午         高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private EiBlock getStatus(boolean isAll)
	{
		EiBlock block = new EiBlock("cmes.cl.status");
		
		EiColumn eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map> list = new ArrayList<Map>();
		
		Map<String, String> _map;
		if(isAll) {
			_map = new HashMap<String, String>();
			_map.put("value", "");
			_map.put("display", "");
			list.add(_map);
		}
		
		_map = new HashMap<String, String>();
		_map.put("value", "停用");
		_map.put("display", "停用");
		list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "在用");
		_map.put("display", "在用");
		list.add(_map);
		
		block.setRows(list);
		return block;
	}
	
	/**
	 * 获得频率单位数据块
	 * <p>Function: getFrequencyUnits</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-3-28下午         高强
	 * </p>
	 *
	 */
    @SuppressWarnings("rawtypes")
	private EiBlock getFrequencyUnits() {
		
        EiBlock block = new EiBlock("cmes.cl.frequencyUnits");
		
		EiColumn eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map> list = new ArrayList<Map>();
		
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("display", "小时");
		_map.put("value", "小时");
		list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("display", "天");
		_map.put("value", "天");
		list.add(_map);
		
		block.setRows(list);
		return block;
	}
	
	/**
	 * 检查是否有重复的巡检路线编号存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-30          高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query("ZMCLSecurityPath.query", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("该巡检路线编号已经存在，请更改！");
			return true;
		}
		return false;
	}
	
	/**
	 * 启用某条数据
	 * <p>Function: modifyStart</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-30下午         高强
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCLSecurityPath.modifyStart");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 停用某条数据
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-30下午         高强
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCLSecurityPath.modifyStop");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 获得巡检工单号
	 * <p>Function: getNoteCode</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7   高强
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private EiInfo getNoteCode(EiInfo info)
	{
		EiBlock resultBlock = info.getBlock(EiConstant.resultBlock);
		for(int i=0; i<resultBlock.getRowCount(); i++)
		{
			String fdKeyId = resultBlock.getRow(i).get("fdKeyId").toString();
			List<Map> resultMap = RoundCheckWorkNote.queryNoteCode(fdKeyId);
			StringBuffer noteCode = new StringBuffer();
			for(int j=0; j<resultMap.size(); j++)
			{
				Map data = resultMap.get(j);
				if(j == resultMap.size()-1)
				{
					noteCode.append(data.get("display"));//如果是引用了一个巡检工单号或者是最后一个巡检工单号不以分号结束
					break;
				}
				noteCode.append(data.get("display")+";");
			}
			resultBlock.getRow(i).put("fdNoteCode", noteCode.toString());
		}
		return info;
	}
	
	/**
	 * 
	 * <p>Function: queryForCustomview</p>
	 * <p>Description: 巡检路线的界面视图customview</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-13下午06:01:44 张小飞
	 * </p>
	 *
	 */
	public EiInfo queryForCustomview(EiInfo inInfo)
	{
		EiInfo outInfo = super.query(inInfo, "ZMCLSecurityPath.queryForCustomview", new ZMCLSecurityPath());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: initCombox</p>
	 * <p>Description: block:Info中的block对象； index:block中数据区的行数（从0开始计）</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-2-15下午05:56:42 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	private String initCombox(EiBlock block, int index){
		Map<String,String> map = block.getRow(index);
		String column = "";
		for(String value : map.keySet()){
			if("value".equals(value)){
				column = map.get(value);
				break;
			}
		}
		return column;
	}
}
